package control
{
	import com.greensock.TweenLite;
	
	import effect.preloader.CircleSlice.CircleSlice;
	
	import events.MyEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import loader.MuiltePicsLoader;
	import loader.SinglePicLoader;
	
	import utils.CenterObjByNumber;
	import utils.MyTimer;
	
	public class SimpleBigPicViewer extends Sprite
	{
		protected var myTimer:MyTimer;
		protected var _wattingTime:uint=3;
		protected var autoPlay:Boolean=false;
		public const ALLLOAD:String = "all_load";
		public const ONEBYONE:String = "one_by_one";
		protected var loadType:String = ALLLOAD;
		protected var max_width:Number = 1366;
		protected var max_height:Number = 768;
		protected var loadCurNum:uint = 0;
		protected var _curNum:int = 0;
		protected var pic_urls:Vector.<String >;
		protected var big_pics:Vector.<Bitmap >;
		protected var allLoaded:Boolean = false;
		protected var singlePicLoader:SinglePicLoader;
		protected var mutilePicLoader:MuiltePicsLoader;
		protected var length:uint = 0;
		protected var background_color:uint;
		protected var background_alpha:Number;
		protected var loading:CircleSlice;
		protected var delayTime:uint=800;
		protected var lastMouseDownTime:uint=0;
		protected var startMouseX:Number;
		protected var startMouseY:Number;
		protected var startX:Number;
		protected var closed:Boolean=false;
		protected var canFlip:Boolean=true;
		protected var flipTimer:MyTimer;
		protected var flipTime:Number=0.5;
		public function SimpleBigPicViewer()
		{
			super();
			
		}
		
		public function get curNum():int
		{
			return _curNum;
		}

		public function set curNum(value:int):void
		{
			_curNum = value;
		}

		public function get wattingTime():uint
		{
			return _wattingTime;
		}

		public function set wattingTime(value:uint):void
		{
			_wattingTime = value;
		}

		public function setType(_loadType:String,_background_color:Number=0,_background_alpha:Number=1):void
		{
			loadType=_loadType;
			background_color=_background_color;
			background_alpha=_background_alpha;
		}
		
		protected function addLoading():void
		{
			if (! loading)
			{
				loading=new CircleSlice();
			}
			
			this.addChild(loading);
			loading.x = max_width / 2;
			loading.y = max_height / 2;
		}
		
		protected function removeLoading():void
		{
			if (loading)
			{
				this.removeChild(loading);
				loading = null;
			}
		}
		
		public function loadPics(vec:Vector.<String>):void
		{
			if(closed)return;
			pic_urls = vec;
			length = vec.length;
			if (loadType==ALLLOAD)
			{
				mutilePicLoader = new MuiltePicsLoader(vec,picsLoaded);
			}
			else
			{
				singlePicLoader = new SinglePicLoader(vec[loadCurNum],picLoaded);
			}
		}
		
		protected function picsLoaded(arr:Array):void
		{
			if(closed)return;
			allLoaded = true;
			removeLoading();
			mutilePicLoader = null;
			if (! big_pics)
			{
				big_pics=new Vector.<Bitmap>();
			}
			for (var i:uint=0; i<arr.length; i++)
			{
				big_pics.push(arr[i].bitmap);
				this.addChild(big_pics[i]);
			}
			start();
			updateDisplay();
			this.dispatchEvent(new MyEvent("picsLoaded"));
		}
		
		protected function picLoaded(bm:Bitmap):void
		{
			if(closed)return;
			if (! big_pics)
			{
				big_pics=new Vector.<Bitmap>();
			}
			big_pics.push(bm);
			this.addChild(bm);
			CenterObjByNumber.centerObj(bm,max_width,max_height);
			if(loadCurNum<curNum)
			{
				bm.x=-bm.width;
			}
			else if(loadCurNum>curNum)
			{
				bm.x=max_width;
			}
			loadCurNum++;
			if (loadCurNum!=length)
			{
				
				
				loadPic();
			}
			else
			{
				allLoaded = true;
				singlePicLoader = null;
				removeLoading();
				start();
				this.dispatchEvent(new MyEvent("picsLoaded"));
			}
			
			
		}
		
		protected function loadPic():void
		{
			if(closed)return;
			singlePicLoader = new SinglePicLoader(pic_urls[loadCurNum],picLoaded);
		}
		
		
		public function setSize(w:Number,h:Number):void
		{
			max_width = w;
			max_height = h;
			this.graphics.clear();
			this.graphics.beginFill(background_color,background_alpha);
			this.graphics.drawRect(0,0,max_width,max_height);
			this.graphics.endFill();
			if (! loading)
			{
				addLoading();
			}
			updateDisplay();
		}
		
		public function autoshow():void
		{
			if(closed)return;
			autoPlay=true;
			addMyTimer();
		}
		
		
		protected function addFlipTimer():void
		{
			if(!flipTimer)
			{
				flipTimer=new MyTimer(setCanFlip,flipTime,1);
				flipTimer.start();
			}
		}
		
		protected function removeFlipTimer():void
		{
			if(flipTimer)
			{
				flipTimer.GC();
				flipTimer=null;
			}
		}
		
		protected function setCanFlip():void
		{
			canFlip=true;
			removeFlipTimer();
		}
		
		protected function addMyTimer():void
		{
			
			if(closed)return;
			this.dispatchEvent(new MyEvent("flipComplete"));
			if(!autoPlay)return;
			removeMyTimer();
			myTimer=new MyTimer(showNext,_wattingTime);
			myTimer.start();
		}
		
		protected function removeMyTimer():void
		{
			if(myTimer)
			{
				myTimer.GC();
				myTimer=null;
			}
		}
		
		protected function showNext():void
		{
			if(closed)return;
			TweenLite.to(big_pics[curNum],0.5,{x:-big_pics[curNum].width});
			curNum++;
			if(curNum==big_pics.length)
			{
				curNum=0;
				
			}
			big_pics[curNum].x=max_width;
			TweenLite.to(big_pics[curNum],0.5,{x:max_width/2-big_pics[curNum].width/2});
		}
		
		
		public function updateDisplay():void
		{
			if(closed)return;
			if(loading)
			{
				loading.x = max_width / 2;
				loading.y = max_height / 2;
			}
			if(allLoaded)
			{
				var bm:Bitmap;
				var length:uint=big_pics.length;
				for(var i:uint=0;i<length;i++)
				{
					bm=big_pics[i];
					CenterObjByNumber.centerObj(bm,max_width,max_height);
					if(i<curNum)
					{
						bm.x=-bm.width;
					}
					else if(i>curNum)
					{
						bm.x=max_width;
					}
				}
			}
		}
		
		protected function start():void
		{
			if(closed)return;
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
			
		}
		
		protected function handleMouseDown(e:MouseEvent):void
		{
			if(!canFlip)return;
			this.dispatchEvent(new MyEvent("moved",null,true));
			if(closed)return;
			if(!allLoaded)return;
			if(autoPlay)
			{
				removeMyTimer();
			}
			startMouseX=e.stageX;
			startMouseY=e.stageY;
			startX=big_pics[curNum].x;
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
		}
		
		protected function handleMouseMove(e:MouseEvent):void
		{
			if(!canFlip)return;
			this.dispatchEvent(new MyEvent("moved",null,true));
			if(closed)return;
			var distance:Number=e.stageX-startMouseX;
			big_pics[curNum].x=startX+distance;
			if(Math.abs(distance)>100)
			{
				showNextImg(distance);
			}
			
		}
		
		public function showNextImg(distance:Number):void
		{
			if(!canFlip)return;
			canFlip=false;
			addFlipTimer();
			if(closed)return;
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			if(distance<0)
			{
				TweenLite.to(big_pics[curNum],0.5,{x:-big_pics[curNum].width});
				curNum++;
				if(curNum==big_pics.length)
				{
					curNum=0;
					
				}
				big_pics[curNum].x=max_width;
				TweenLite.to(big_pics[curNum],0.5,{x:max_width/2-big_pics[curNum].width/2,onComplete:addMyTimer});
			}
			else
			{
				TweenLite.to(big_pics[curNum],0.5,{x:max_width});
				curNum--;
				if(curNum<0)
				{
					curNum=big_pics.length-1;
					
				}
				big_pics[curNum].x=-big_pics[curNum].width;
				TweenLite.to(big_pics[curNum],0.5,{x:max_width/2-big_pics[curNum].width/2,onComplete:addMyTimer});
			}
			if(autoPlay)
			{
				addMyTimer();
			}
		}
				
		
		protected function handleMouseUp(e:MouseEvent):void
		{
			this.dispatchEvent(new MyEvent("moved",null,true));
			if(closed)return;
			var endMouseY:Number=e.stageY;
			var endMouseX:Number=e.stageX;
			var nowTime:uint=getTimer();
			if(lastMouseDownTime==0)
			{
				lastMouseDownTime=nowTime;
			}
			else
			{
				if(nowTime-lastMouseDownTime <delayTime && Math.abs(endMouseY-startMouseY)<10 && Math.abs(endMouseX-startMouseX)<10)
				{
					lastMouseDownTime=0;
					trace("close");
					this.dispatchEvent(new MyEvent("thisCanClose"));
				}
				else
				{
					lastMouseDownTime=getTimer();
				}
			}
			if(big_pics)
			{
				TweenLite.to(big_pics[curNum],0.5,{x:startX});
			}
			if(this.stage)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			}
			
		}
		
		
		public function gc():void
		{
			closed=true;
			if(this.stage.hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
			}
			if(this.stage.hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			}
			removeMyTimer();
			removeFlipTimer();
			if(big_pics)
			{
				for(var i:uint=0;i<big_pics.length;i++)
				{
					big_pics[i].bitmapData.dispose();
				}
				big_pics=null;
			}
		}
		
	}
}